package com.itcast.order.controller;

import com.itcast.order.entity.Product;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
@RequestMapping("/order")
/**
 * 全局熔断配置
 */
@DefaultProperties(defaultFallback = "defaultFallback")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;
    /**
     * 配置熔断保护
     */
    //@HystrixCommand(fallbackMethod = "orderFallBack")
    @GetMapping("/buy/{id}")
    public Product findById(@PathVariable Long id) throws InterruptedException {
        Thread.sleep(2000);
        Product product = restTemplate.getForObject("http://127.0.0.1:9001product/" + id, Product.class);
        return product;
    }

    public Product defaultFallback() {
        Product product = new Product();
        product.setProductName("触发降级方法");
        return product;
    }

    /**
     * hystrix降级逻辑
     */
//    public Product orderFallBack(Long id) {
//        Product product = new Product();
//        product.setProductName("触发降级方法");
//        return product;
//    }
}
